package com.wzx.clusterserver;

import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    public static void main(String[] args) {
        String encode = new BCryptPasswordEncoder().encode("root");
        System.out.println(encode);
    }

    @Override
    public void configure(HttpSecurity http) throws Exception {
//        super.configure(web);
        http
                .csrf().disable()
                .formLogin()
                .loginPage("/user/login")
                .loginProcessingUrl("/login.do")
                .defaultSuccessUrl("/user/info")
                .usernameParameter("username")
                .passwordParameter("password")
                .and()
                .authorizeRequests()
                .mvcMatchers("/user/login", "/static/**")
                .permitAll().anyRequest().authenticated()
        ;
    }

    @Bean
    public UserDetailsService userDetailsService() {
        InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
        manager.createUser(User.withDefaultPasswordEncoder().username("123").password("123").roles("USER").build());
        return manager;
    }
}
